package com.sdry.web.controller.lz;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.lz.DeviceBreakdown;
import com.sdry.model.lz.DeviceMaintenanceRecordsDetail;
import com.sdry.model.lz.LzQueryCriteria;
import com.sdry.service.lz.DeviceBreakdownService;
import com.sdry.service.lz.MaintResumeService;
import com.sdry.utils.PushExample;
import com.sdry.utils.ResponseUtil;
import com.sdry.utils.StringUtil;

/**
 * @ClassName DeviceBreakdownController
 * @Description 设备故障维修单
 * @Author lz
 * @Date 2018年11月1日 10:19:34
 * @Version 1.0
 */
@Controller
@RequestMapping("breakdown")
public class DeviceBreakdownController {
	
	public static final Integer AFFECT_ROW = 0;
    @Resource
    DeviceBreakdownService deviceBreakdownService;
    @Resource
    MaintResumeService maintResumeService;

    /**
     * 添加设备故障维修单
     * @param response
     * @param deviceBreakdown 设备故障维修单实体
     * @throws Exception
     */
    @RequestMapping("/addDeviceBreakdown")
    public void addDeviceBreakdown(HttpServletResponse response, DeviceBreakdown deviceBreakdown) throws Exception {
        if(deviceBreakdown != null){
            if(StringUtil.isEmpty(deviceBreakdown.getRepairTime())){
                deviceBreakdown.setRepairTime(null);
            }
            if(StringUtil.isEmpty(deviceBreakdown.getFixStartTime())){
                deviceBreakdown.setFixStartTime(null);
            }
            if(StringUtil.isEmpty(deviceBreakdown.getFixEndTime())){
                deviceBreakdown.setFixEndTime(null);
            }
            if(StringUtil.isEmpty(deviceBreakdown.getAcceptTime())){
                deviceBreakdown.setAcceptTime(null);
            }
        }
        String people = deviceBreakdown.getApprovalPeople();
        deviceBreakdown.setDeviceStatus("0");
        deviceBreakdown.setApprovalStatus("0");
        /*
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        deviceBreakdown.setApprovalTime(sdf.format(new Date()));
        */
    	Long affectRow1 = deviceBreakdownService.addDeviceBreakdown(deviceBreakdown);
        if (affectRow1 > AFFECT_ROW) {
            int testSendPush = PushExample.testSendPush("您有一个新的设备报修单", "您有一个新的设备报修单需要审批", people, "1");
            System.err.println(testSendPush);
            ResponseUtil.write(response, affectRow1);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 添加设备维修申请登记详细
     * @param deviceMaintenanceRecordsDetail
     */
    @RequestMapping("/addDeviceMaintenanceRecordsDetail")
    public void addDeviceMaintenanceRecordsDetail(DeviceMaintenanceRecordsDetail deviceMaintenanceRecordsDetail){
        maintResumeService.addDeviceMaintenanceRecordsDetail(deviceMaintenanceRecordsDetail);
    }

    /**
     * 单个删除和批量删除
     * @param response
     * @param idArr id数组
     * @throws Exception
     */
    @RequestMapping("/deleteDeviceBreakdownById")
    public void deleteDeviceBreakdownById(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr) throws Exception {
        for (int i = 0; i < idArr.length; i++) {
            Long affectRow = deviceBreakdownService.deleteDeviceBreakdownById(Long.parseLong(idArr[i]));
            if (affectRow > AFFECT_ROW) {
                ResponseUtil.write(response, affectRow);
            } else {
                ResponseUtil.write(response, AFFECT_ROW);
            }
        }
    }

    /**
     * 根据ID修改设备信息
     * @param response
     * @param deviceBreakdown 选中行的设备故障维修单
     * @throws Exception
     */
    @RequestMapping("/editDeviceBreakdownById")
    public void editDeviceBreakdownById(HttpServletResponse response, DeviceBreakdown deviceBreakdown) throws Exception {
        if(deviceBreakdown != null){
            if(StringUtil.isEmpty(deviceBreakdown.getRepairTime())){
                deviceBreakdown.setRepairTime(null);
            }
            if(StringUtil.isEmpty(deviceBreakdown.getFixStartTime())){
                deviceBreakdown.setFixStartTime(null);
            }
            if(StringUtil.isEmpty(deviceBreakdown.getFixEndTime())){
                deviceBreakdown.setFixEndTime(null);
            }
            if(StringUtil.isEmpty(deviceBreakdown.getAcceptTime())){
                deviceBreakdown.setAcceptTime(null);
            }
        }
        Long affectRow = deviceBreakdownService.editDeviceBreakdownById(deviceBreakdown);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 分页查询设备故障维修单
     * @param response
     * @param criteria 分页查询条件
     */
    @RequestMapping("/queryDeviceBreakdown")
    public void queryDeviceBreakdown(HttpServletResponse response, LzQueryCriteria criteria){
        String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<DeviceBreakdown> deviceList = deviceBreakdownService.queryDeviceBreakdown(criteria);
        int count = deviceBreakdownService.countDeviceBreakdown(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(deviceList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    /**
     * 确认维修完成
     * @param response
     * @param id id
     * @throws Exception
     */
    @RequestMapping("/confirmRepairFinish")
    public void confirmRepairFinish(HttpServletResponse response, String id) throws Exception {
        Long affectRow = deviceBreakdownService.confirmRepairFinish(Long.parseLong(id));
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }


    /*@RequestMapping("/queryLoopById")
    public void queryLoopById(HttpServletResponse response, HttpSession session, String id) throws Exception {
        List<DeviceMaintenanceRecordsDetail> detailList = deviceBreakdownService.queryLoopById(Long.parseLong(id));
        if (detailList.size() > AFFECT_ROW) {
            session.setAttribute("detailList", detailList);
        }
        if (detailList.size() > AFFECT_ROW) {
            JSONArray jsonArray = JSONArray.fromObject(detailList);
            ResponseUtil.write(response, jsonArray);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }*/


    /**
     * 分页查询设备维修申请审批
     * @param response
     * @param criteria
     */
    @RequestMapping("/queryDeviceBreakdownApproval")
    public void queryDeviceBreakdownApproval(HttpServletResponse response, LzQueryCriteria criteria){
        String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<DeviceBreakdown> deviceList = deviceBreakdownService.queryDeviceBreakdownApproval(criteria);
        int count = deviceBreakdownService.countDeviceBreakdownApproval(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(deviceList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 点击审批更改状态为1
     * @param response
     * @param deviceBreakdown 审批参数
     * @throws Exception
     */
    @RequestMapping("/updateApprovalStatusById")
    public void updateApprovalStatusById(HttpServletResponse response, DeviceBreakdown deviceBreakdown) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        deviceBreakdown.setApprovalTime(sdf.format(new Date()));
        Long affectRow = deviceBreakdownService.updateApprovalStatusById(deviceBreakdown);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 点击审批更改状态为2
     * @param response
     * @param deviceBreakdown 审批参数
     * @throws Exception
     */
    @RequestMapping("/updateApprovalStatusById2")
    public void updateApprovalStatusById2(HttpServletResponse response, DeviceBreakdown deviceBreakdown) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        deviceBreakdown.setApprovalTime(sdf.format(new Date()));
        Long affectRow = deviceBreakdownService.updateApprovalStatusById2(deviceBreakdown);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 安卓接口:查询抄送人推送
     * @param response
     * @throws Exception
     */
    @RequestMapping("/queryCCPeopleMSG")
    public void queryCCPeopleMSG(HttpServletResponse response) throws Exception {
        List<DeviceBreakdown> deviceBreakdownList = deviceBreakdownService.queryCCPeopleMSG();
        JSONObject jsonObject = new JSONObject();
        if(deviceBreakdownList.size() > 0){
            jsonObject.put("state", 1);
            jsonObject.put("message", "成功");
            jsonObject.put("data", deviceBreakdownList);
            ResponseUtil.write(response, jsonObject);
        } else {
            jsonObject.put("state", 0);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "");
            ResponseUtil.write(response, jsonObject);
        }
    }
}
